package com.project.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.entity.ApprovalSteps;
import com.project.entity.Approvals;
import com.project.vo.ApprovalInfoVO;
import com.project.enums.ApprovalTypeEnum;
import com.project.enums.DecisionEnum;
import com.project.mapper.ApprovalStepsMapper;
import com.project.mapper.ApprovalsMapper;
import com.project.mapper.UserMapper;

import com.project.mapper.ApprovalStepsMapper;
import com.project.service.ApprovalStepsService;
import com.project.vo.ApprovalStepDetailVO;
import com.project.vo.ApprovalStepListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

@Service
public class ApprovalStepsServiceImpl extends ServiceImpl<ApprovalStepsMapper, ApprovalSteps> implements ApprovalStepsService {
    @Autowired
    private ApprovalStepsMapper approvalStepsMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ApprovalsMapper approvalsMapper;

    /**
     * 根据步骤ID查询审批步骤记录
     * @param id 审批步骤id
     * @return
     */
    @Override
    public ApprovalStepDetailVO getByStepId(Integer id) {
        ApprovalStepDetailVO approvalStepDetailVO = new ApprovalStepDetailVO();
        //封装外层
       // ApprovalSteps approvalSteps = getById(id);
        ApprovalSteps approvalSteps = approvalStepsMapper.getByStepId(id);
        String realName = userMapper.selectById(approvalSteps.getApproverId())
                                    .getRealName();
        BeanUtils.copyProperties(approvalSteps,approvalStepDetailVO);
        approvalStepDetailVO.setApproverName(realName);
        Integer decision = approvalSteps.getDecision();
        //0表示未审批
        approvalStepDetailVO.setDecisionName(DecisionEnum.getStatusName(decision));
//        if(decision ==null)
//        {
//            approvalStepDetailVO.setDecisionName("未审批");
//        }else{
//            approvalStepDetailVO.setDecisionName(DecisionEnum.getStatusName(decision));
//        }

        //封装内层vo
        Approvals approvals=approvalsMapper.selectById(approvalSteps.getApprovalId());
        ApprovalInfoVO approvalInfoVO=new ApprovalInfoVO();
        Integer approvalType = approvals.getApprovalType();
        approvalInfoVO.setApprovalType(approvalType);
        approvalInfoVO.setApprovalTypeName(ApprovalTypeEnum.getStatusName(approvalType));
        approvalInfoVO.setCurrentStep(approvals.getCurrentStep());

        //总封装
        approvalStepDetailVO.setApprovalInfo(approvalInfoVO);
        return approvalStepDetailVO;
    }

    /**
     * 根据实例id获取步骤列表
     * @param id
     * @return
     */
    @Override
    public List<ApprovalStepListVO> getListByApprovalId(Integer id) {
        List<ApprovalStepDetailVO> list=new ArrayList<>();
        Integer currentStep = approvalsMapper.selectById(id)
                                             .getCurrentStep();
        List<ApprovalStepListVO> stepList=approvalStepsMapper.getByApprovalId(id);
        stepList.forEach(new Consumer<ApprovalStepListVO>() {
            @Override
            public void accept(ApprovalStepListVO step) {
                step.setApproverName(userMapper.selectById(step.getApproverId()).getRealName());
                if(step.getDecision()==null){
                    step.setDecisionName("待审批");
                }else{
                    step.setDecisionName(DecisionEnum.getStatusName(step.getDecision()));
                }
                step.setIsCurrent(currentStep.equals(step.getStepOrder()));
            }
        });
        return stepList;
    }
}
